<template>
  <div>
    <div class="picker">
      <div class="pickerInner" @click="isPickerShow = true">
        {{ pickerText }}
      </div>
    </div>

    <div class="swiper">
      <van-swipe class="my-swipe" :autoplay="3000" indicator-color="white">
        <van-swipe-item v-for="(item, index) in swiperList" :key="index">
          <img :src="item" alt="">
        </van-swipe-item>
      </van-swipe>
    </div>

    <div class="listTab">
      <p
        v-for="(item, index) in tabList"
        :key="index"
        :class="{ active: tabIndex == item.id }"
        @click="tabIndex = item.id"
      >
        {{ item.name }}
      </p>
    </div>

    <div
      v-infinite-scroll="loadMore"
      infinite-scroll-disabled="busy"
      infinite-scroll-distance="10"
    >
      <div v-if="list.length > 0" class="list">
        <div
          v-for="(item, index) in list"
          :key="index"
          class="item"
        >
          <img :src="item.image" alt="">
          <div class="itemInfo">
            <div class="tit">{{ item.name }}</div>
            <div class="typeList">
              <p>
                {{ item.cropId == 1 ? "柑橘" : "茶叶" }}
              </p>
              <p
                v-if="item.dangerLevel"
                :class="{ main: item.dangerLevel == 1 }"
              >
                {{ item.dangerLevel == 1 ? "主要" : "次要" }}
              </p>
            </div>
            <div class="text">{{ item.occurrenceLaw }}</div>
          </div>
        </div>
      </div>
      <van-empty v-else description="暂无数据..." />
      <div v-if="!isMore && list.length > 0" class="noMore">~ 暂无更多 ~</div>
    </div>

    <van-popup v-model="isPickerShow" round>
      <div class="popup">
        <div class="tit">生育期选择</div>
        <ul>
          <li
            v-for="(item, index) in pickerList"
            :key="index"
            @click="onConfirm(item)"
          >
            {{ item.growthPeriodName }}
          </li>
        </ul>
        <div class="btn">
          <van-button
            type="primary"
            @click="isPickerShow = false"
          >取消</van-button>
          <van-button
            type="primary"
            @click="isPickerShow = false"
          >确定</van-button>
        </div>
      </div>
    </van-popup>
  </div>
</template>

<script>
import {
  pestControlGrowthPeriodList,
  pestControlInfoMList
} from '@/api';
export default {
  data() {
    return {
      isPickerShow: false,
      pickerText: '',
      pickerId: '',
      pickerList: [],

      swiperList: [
      ],

      tabList: [
        {
          id: 1,
          name: '虫害'
        },
        {
          id: 2,
          name: '病害'
        }
      ],
      tabIndex: 1,

      busy: false,

      isMore: true, // 下页是否有数据
      list: [],
      page: {
        pageSize: 10,
        pageNum: 1,
        total: 0
      }
    };
  },
  computed: {
    cropId() {
      return this.$route.query.cropId;
    }
  },
  watch: {
    tabIndex() {
      this.list = [];
      this.page.pageNum = 1;
      this.getMlist();
    }
  },
  mounted() {
    this.$nextTick(() => {
      this.getPeriodList();
    });
  },
  methods: {
    loadMore() {
      if (this.list.length > 0) {
        this.busy = true;
        const current = this.page.pageNum;
        this.page.pageNum = current + 1;
        this.getMlist();
      }
    },
    async getPeriodList() {
      const result = await pestControlGrowthPeriodList();
      if (result.code == 200) {
        this.pickerList = result.rows;
        this.pickerText = result.rows[0].growthPeriodName;
        this.pickerId = result.rows[0].growthId;
        this.getMlist();
      }
    },
    async getMlist() {
      const type = this.tabIndex;
      const growthPeriodId = this.pickerId;
      const data = {
        type,
        growthPeriodId,
        pageNum: this.page.pageNum,
        pageSize: this.page.pageSize
      };
      if (this.cropId) {
        data.cropSubId = this.cropId;
      }
      const result = await pestControlInfoMList(data);
      if (result.code == 200) {
        const reslist = result.rows;
        this.page.total = result.total;
        if (reslist.length > 0) {
          this.list = this.list.concat(reslist);
          this.isMore = true;
          this.busy = false;
        } else {
          if (this.page.pageNum != 1) {
            this.isMore = false;
          }
        }
      }
    },
    onConfirm(item) {
      this.pickerText = item.growthPeriodName;
      this.pickerId = item.growthId;
      this.isPickerShow = false;
      this.list = [];
      this.page.pageNum = 1;
      this.getMlist();
    }
  }
};
</script>

<style lang="scss" scoped>
.picker {
  display: flex;
  align-items: center;
  justify-content: center;
  .pickerInner {
    height: 42px;
    display: flex;
    align-items: center;
    font-size: 18px;
    font-weight: 400;
    color: #1a1a1a;
    white-space: nowrap;
    img {
      width: 10px;
      margin-left: 5px;
    }
  }
}
.swiper {
  margin: 0 15px;
  border-radius: 8px;
}
.listTab {
  display: flex;
  align-items: center;
  margin-top: 7px;
  border-bottom: 1px solid #dedede;
  p {
    line-height: 40px;
    margin: 0 16px;
    font-size: 16px;
    font-weight: 400;
    color: #666666;
    position: relative;
  }
  p.active {
    color: #ffa10b;
  }
  p.active::after {
    content: "";
    width: 28px;
    height: 2px;
    background: #ffa10b;
    border-radius: 1px;
    position: absolute;
    bottom: 0;
    left: 50%;
    transform: translateX(-50%);
  }
}
.noMore {
  text-align: center;
}
.list {
  padding: 0 15px;
  .item {
    padding: 15px 0;
    border-bottom: 1px solid #dedede;
    display: flex;
    align-items: center;
    img {
      width: 103px;
      height: 78px;
    }
    .itemInfo {
      width: 66%;
      margin-left: 12px;
      .tit {
        font-size: 16px;
        font-weight: 400;
        color: #1a1a1a;
      }
      .typeList {
        display: flex;
        align-items: center;
        margin-top: 5px;
        p {
          padding: 0 5px;
          height: 17px;
          background: #ffffff;
          border-radius: 2px;
          border: 1px solid #8f8f8f;
          margin-right: 5px;
          font-size: 10px;
          font-weight: 400;
          color: #8f8f8f;
          display: flex;
          align-items: center;
          justify-content: center;
        }
        p.main {
          border: 1px solid #ffa10b;
          color: #ffa10b;
        }
      }
      .text {
        font-size: 14px;
        font-weight: 400;
        color: #504f4f;
        margin-top: 14px;
        overflow: hidden;
        text-overflow: ellipsis;
        white-space: nowrap;
      }
    }
  }
  .item:last-child {
    border-bottom: none;
  }
}
.popup {
  width: 325px;
  background: #ffffff;
  border-radius: 6px;
  .tit {
    padding-top: 21px;
    font-size: 16px;
    font-weight: 500;
    color: #333333;
    text-align: center;
  }
  ul {
    padding: 0 12px;
    li {
      height: 47px;
      border-bottom: 1px solid #dedede;
      display: flex;
      align-items: center;
      justify-content: center;
      font-size: 16px;
      font-weight: 400;
      color: #333333;
    }
  }
  .btn {
    display: flex;
    align-items: center;
    justify-content: center;
    margin-top: 17px;
    margin-bottom: 20px;
    .van-button {
      width: 70px;
      height: 32px;
      background: #dedede;
      border-radius: 4px;
      font-size: 14px;
      font-weight: 500;
      color: #7b7b7b;
      padding: 0;
      border: none;
      margin: 0 6px;
    }
    .van-button:last-child {
      background: #ffa10b;
      color: #ffffff;
    }
  }
}
</style>
